package com.life.notes.controller;

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.common.collect.Lists;
import com.life.notes.config.utils.BaseResponse;
import com.life.notes.config.utils.ResponseMsgUtil;
import com.life.notes.config.utils.TreeBuilderUtil;
import com.life.notes.dto.MenuQueryPara;
import com.life.notes.model.SysMenuEntity;
import com.life.notes.service.SysMenuService;
import com.life.notes.vo.MenuTreeNode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;


/**
 * <p> 系统管理-菜单表  接口 </p>
 *
 * @author: zhengqing
 * @description:
 * @date: 2019-08-19
 *
 */
@RestController
@RequestMapping("/api/system/menu")
@Api(description = "系统管理 - 菜单表 接口")
public class SysMenuController {

    @Autowired
    SysMenuService sysMenuService;

    @PostMapping(value = "/treeMenu", produces = "application/json;charset=utf-8")
    @ApiOperation(value = "获取菜单树", httpMethod = "POST", response = BaseResponse.class)
    public BaseResponse treeMenu() {
        List<SysMenuEntity> list = sysMenuService.listTreeMenu();
        List<MenuTreeNode> menuTreeNodeList = Lists.newArrayList();
        if( list != null && !list.isEmpty() ){
            list.forEach( temp->{
                MenuTreeNode menuTreeNode = new MenuTreeNode();
                BeanUtil.copyProperties( temp, menuTreeNode);
                menuTreeNodeList.add( menuTreeNode );
            } );
        }
        List<MenuTreeNode> menuTreeNodeList2 = TreeBuilderUtil.buildMenuTree( menuTreeNodeList );

        menuTreeNodeList2.stream().sorted( Comparator.comparing( MenuTreeNode::getSortNo ) ).collect( Collectors.toList());
        JSONObject json = new JSONObject();
        json.put( "menuList", list);
        json.put( "menuTree", menuTreeNodeList2);
        return ResponseMsgUtil.success(json);
    }

    @PostMapping(value = "/save", produces = "application/json;charset=utf-8")
    @ApiOperation(value = "保存菜单 ", httpMethod = "POST", response = BaseResponse.class)
    public BaseResponse save(@RequestBody @Validated SysMenuEntity input) {
        Integer id = sysMenuService.saveMenu(input);
        return ResponseMsgUtil.success(id);
    }

    @PostMapping(value = "/delete", produces = "application/json;charset=utf-8")
    @ApiOperation(value = "删除菜单", httpMethod = "POST", response = BaseResponse.class)
    public BaseResponse delete(@RequestBody MenuQueryPara input) {
        // 如果该菜单下存在子菜单，提示先删除子菜单
        List<SysMenuEntity> menuList = sysMenuService.list(new QueryWrapper<SysMenuEntity>().eq("parent_id", input.getId()));
        if (!CollectionUtils.isEmpty(menuList)){
//            menuList.forEach(e -> menuService.deleteById(e.getId()));
            return ResponseMsgUtil.error("该菜单下存在子菜单，请先删除子菜单！");
        }
        sysMenuService.removeById(input.getId());
        return ResponseMsgUtil.success();
    }

    // 下面暂时不用 ================================================

    @PostMapping(value = "/listPage", produces = "application/json;charset=utf-8")
    @ApiOperation(value = "获取系统管理-菜单表 列表分页", httpMethod = "POST", response = BaseResponse.class)
    public BaseResponse listPage(@RequestBody MenuQueryPara filter) {
        return ResponseMsgUtil.success(sysMenuService.listPage(filter));
    }

    @PostMapping(value = "/list", produces = "application/json;charset=utf-8")
    @ApiOperation(value = "获取系统管理-菜单表 列表", httpMethod = "POST", response = BaseResponse.class)
    public BaseResponse list(@RequestBody MenuQueryPara filter) {
        List<SysMenuEntity> result = sysMenuService.list(filter);
        return ResponseMsgUtil.success(result);
    }

    @PostMapping(value = "/getById", produces = "application/json;charset=utf-8")
    @ApiOperation(value = "获取系统管理-菜单表 信息", httpMethod = "POST", response = BaseResponse.class)
    public BaseResponse getById(@RequestBody MenuQueryPara input) {
        SysMenuEntity entity = sysMenuService.getById(input.getId());
        return ResponseMsgUtil.success(entity);
    }

}
